/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Marc
 */

import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import aima.search.framework.GraphSearch;
import aima.search.framework.Problem;
import aima.search.framework.Search;
import aima.search.framework.SearchAgent;
import aima.search.framework.TreeSearch;
import aima.search.informed.AStarSearch;
import aima.search.informed.HillClimbingSearch;
import aima.search.uninformed.BreadthFirstSearch;
import aima.search.uninformed.DepthLimitedSearch;
import aima.search.uninformed.IterativeDeepeningSearch;
import aima.search.informed.IterativeDeepeningAStarSearch;
import aima.search.informed.SimulatedAnnealingSearch;
import javax.swing.JCheckBox;
import javax.swing.JTextField;


public class CarsJFrame1 extends javax.swing.JFrame {

    /** Creates new form ProbIA15JFrame */
    public CarsJFrame1() {
        initComponents();
    }

    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        Ejecutar = new javax.swing.JButton();
        Salir = new javax.swing.JButton();
	Heuristica = new JCheckBox("Heuristica1", true);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(jLabel1.getFont());
        jLabel1.setText("Problema 15");

        jTextArea1.setColumns(20);
        jTextArea1.setFont(jTextArea1.getFont());
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        Ejecutar.setFont(Ejecutar.getFont());
        Ejecutar.setText("Ejecutar");
        Ejecutar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                EjecutarMouseClicked(evt);
            }
        });

        Salir.setFont(Salir.getFont());
        Salir.setText("Salir");
        Salir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                SalirMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(162, 162, 162)
                        .add(Ejecutar)
                        .add(23, 23, 23)
                        .add(Salir))
                    .add(layout.createSequentialGroup()
                        .add(196, 196, 196)
                        .add(jLabel1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jLabel1)
                .add(18, 18, 18)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 370, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(Salir)
                    .add(Ejecutar))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    private void SalirMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
                     dispose();
    }

    private void EjecutarMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
        State CS =new State(50, 25);
        CarsSimulatedAnnealingSearch(CS);
        CarsHillClimbingSearch(CS);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CarsJFrame1().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton Ejecutar;
    private javax.swing.JButton Salir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private JTextField lambda;
    private JCheckBox Heuristica;
    // End of variables declaration

    private void CarsHillClimbingSearch(State CS) {
        System.out.println("\nTSP HillClimbing  -->");
        try {
            Problem problem =  new Problem(CS,new CarsSuccessorFunction(), new CarsGoalTest(),new CarsHeuristicFunction2());
            Search search =  new HillClimbingSearch();
            SearchAgent agent = new SearchAgent(problem,search);

            System.out.println();
            printActions(agent.getActions());
            printInstrumentation(agent.getInstrumentation());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void CarsSimulatedAnnealingSearch(State CS) {
        System.out.println("\nTSP Simulated Annealing  -->");
        try {
            Problem problem =  new Problem(CS,new CarsSuccessorFunctionSA(), new CarsGoalTest(),new CarsHeuristicFunction());
            SimulatedAnnealingSearch search =  new SimulatedAnnealingSearch(60000,100,1,0.001);
            //search.traceOn();
            SearchAgent agent = new SearchAgent(problem,search);
            System.out.println();
            printActions(agent.getActions());
            printInstrumentation(agent.getInstrumentation());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void printInstrumentation(Properties properties) {
        Iterator keys = properties.keySet().iterator();
        jTextArea1.append("\n\n");
        while (keys.hasNext()) {
            String key = (String) keys.next();
            String property = properties.getProperty(key);
            jTextArea1.append(key + " : " + property+"\n");
            //System.out.println(key + " : " + property);
        }
         jTextArea1.append("\n\n");
    }

    private void printActions(List actions) {
        for (int i = 0; i < actions.size(); i++) {
            String action = (String) actions.get(i);
            //System.out.println(action);
            jTextArea1.append(action+"\n");
        }
    }

}

